﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace TaskClient4
{
    public partial class TestChildWindow : ChildWindow
    {
        private readonly ManualResetEvent _manualResetEvent;

        public TestChildWindow()
        {
            InitializeComponent();
            _manualResetEvent = new ManualResetEvent(false);
            Closed += (_, __) => _manualResetEvent.Set();
        }

        public bool? ShowSync()
        {
            if (Dispatcher.CheckAccess())
                throw new InvalidOperationException("ShowSync只能使用后台线程调用！");

            Dispatcher.BeginInvoke(Show);
            _manualResetEvent.Reset();
            _manualResetEvent.WaitOne();
            return DialogResult;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}

